function Queue() {
    let items = [];

    // 添加元素
    this.enqueue = function (element) {
        items.push(element)
    }
    // 移除元素
    this.dequeue = function () {
        return items.shift()
    }
    // 查看队列头元素
    this.front = function () {
        return items[0]
    }
    // 检测队列是否为空
    this.isEmpty = function () {
        return items.length == 0;
    }
    // 打印队列元素
    this.print = function () {
        console.log(items.toString())
    }
    // 长度
    this.size = function () {
        return items.length
    }
}


// es6实现
let Queue2 = (function () {
    const items = new WeakMap()
    class Queue2{
        constructor(){
            items.set(this,[])
        }
        enqueue(element){
            let q = items.get(this)
            q.push(element)
        }
        dequeue(){
            let q = items.get(this)
            let r = q.shift();
            return r
        }
    }
    return Queue2;
})();

let queue = new Queue();
console.log(queue.isEmpty());

queue.enqueue("John")
queue.enqueue("Jack")
queue.enqueue("Camila")

queue.print();
console.log(queue.size())
console.log(queue.isEmpty())

// 优先队列
// 医院急诊科候诊室，医生会优先处理病情比较严重的患者，通常，护士会鉴别分类，根据患者病情严重程度放号
// 实现一个优先队列，有两种选项:设置优先级，然后再正确的位置添加元素，或者用入列操作添加元素，然后按照优先级移除他们

function PriorityQueue(){
    let items = []
    function QueueElement(element,priority) {
        this.element = element;
        this.priority = priority
        console.log(QueueElement,"##############")
    }
    this.enqueue = function(element,priority) {
        let queueElement = new QueueElement(element,priority)
        let added = false;
        for(let i=0;i<items.length;i++){
            if(queueElement.priority < items[i].priority){
                items.splice(i,0,queueElement)
                added = true
                break
            }
        }
        if(!added){
            items.push(queueElement);
        }
    };
    this.print = function () {
        for(let i = 0;i<items.length;i++){
            console.log(`${items[i].element}-${items[i].priority}`);
        }
    }
    // 其他方法和默认饿Queue实现相同
}

// 击鼓传花 循环队列
function hotPotato(nameList,num){
    let queue = new Queue();
    for(let i = 0;i<nameList.length;i++){
        queue.enqueue(nameList[i])
    }
    let eliminated = '';
    while (queue.size() > 1){
        for(let i =0;i<num;i++){
            queue.enqueue(queue.dequeue())
        }
        eliminated = queue.dequeue()
        console.log(eliminated + '在击鼓传花游戏被淘汰')
    }
    return queue.dequeue()
}

let names = ['John','Jack','Camila','Ingrid','Carl'];
let winner = hotPotato(names,7);
console.log("the winner is " + winner)


